//
//  CustomView.m
//  Target-Actions
//
//  Created by apple on 15/9/12.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "CustomView.h"

@interface CustomView()

{
    UILabel *_titleLbel;
}

@end

@implementation CustomView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _titleLbel = [[UILabel alloc] initWithFrame:self.bounds];
        
        _titleLbel.textColor = [UIColor blackColor];
        
        [self addSubview:_titleLbel];
    }
    
    return self;
}

- (void)setTitle:(NSString *)title
{
    _title = [title copy];
    
    _titleLbel.text = title;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _titleLbel.textColor = [UIColor lightGrayColor];
        
    _state = CustomViewStateHighlighted;
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    CGPoint location = [touch locationInView:self];
    
    //判断点point在不在矩形当中
    
    if (!CGRectContainsPoint(_titleLbel.bounds, location))
    {
        _titleLbel.textColor = [UIColor blackColor];
        
        _state = CustomViewStateNormal;
    }
    
   else
    
   {
       _titleLbel.textColor = [UIColor redColor];
    
       _state = CustomViewStateHighlighted;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_state == CustomViewStateHighlighted)
    {
        if (_touchUpInside)
        {
            _touchUpInside(self);
        }
    }
    
    _titleLbel.textColor = [UIColor blackColor];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    _titleLbel.textColor = [UIColor whiteColor];
}

@end
